package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.mapper.StudentMapper;
import com.itheima.pojo.PageResult;
import com.itheima.pojo.Student;
import com.itheima.pojo.StudentQueryParam;
import com.itheima.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    StudentMapper sm;


    @Override
    public PageResult<Student> getStudents(StudentQueryParam sqp) {
        PageHelper.startPage(sqp.getPage(),sqp.getPageSize());
        List<Student> slist=sm.getStudents(sqp);
        Page<Student> p=(Page)slist;
        return new PageResult<>(p.getTotal(),p.getResult());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addStudents(Student s) {
        s.setCreateTime(LocalDateTime.now());
        s.setUpdateTime(LocalDateTime.now());
        s.setViolationCount((short)0);
        s.setViolationScore((short)0);
        sm.addStudents(s);
    }

    @Override
    public Student getStuById(Integer id) {
       Student idstu= sm.getStuById(id);
        return idstu;
    }

    @Override
    public void updateStu(Student s) {
        s.setUpdateTime(LocalDateTime.now());
        sm.updateStu(s);
    }

    @Override
    public void deleteStu(List<Integer> l) {
        sm.deleteStu(l);
    }

    @Override
    public void viji(Integer id,Integer score) {
        sm.viji(id,score);
    }
}
